import { GET_CHANNEL_LIST, GET_ARTICLE_LIST } from "../constants"
import {
  getChannelListApi,
  getArticleListApi,
  saveArticleApi,
} from "@/api/article"
import {
  FETCH_DATA_REQUEST,
  FETCH_DATA_SUCCESS,
  FETCH_DATA_FAILURE,
} from "../constants"
import {
  delAriticleApi,
  editArticleApi,
  getArticleInfoApi,
} from "../../api/article"

//获取频道列表任务
export const getChannelList = () => {
  return async (dispatch) => {
    dispatch({ type: FETCH_DATA_REQUEST })
    try {
      const res = await getChannelListApi()
      dispatch({ type: FETCH_DATA_SUCCESS })
      dispatch({
        type: GET_CHANNEL_LIST,
        payload: res.data.data.channels,
      })
      // console.log("channels", res)
    } catch (error) {
      dispatch({ type: FETCH_DATA_FAILURE, payload: error.message })
    }
  }
}

//获取文章列表任务
export const getArticleList = (params) => {
  return async (dispatch, getState) => {
    try {
      dispatch({ type: FETCH_DATA_REQUEST })
      const res = await getArticleListApi(params)
      // console.log("res7410852", res)
      dispatch({ type: FETCH_DATA_SUCCESS })
      dispatch({
        type: GET_ARTICLE_LIST,
        payload: res.data.data,
      })
    } catch (error) {
      dispatch({ type: FETCH_DATA_FAILURE, payload: error.message })
    }
  }
}

//删除文章列表任务

export const delArticle = (id) => {
  return async () => {
    await delAriticleApi(id)
  }
}

//发布文章接口
export const saveArticle = (draft, data) => {
  return async () => {
    await saveArticleApi(draft, data)
  }
}

//获取文章详情信息
export const getArticleInfo = (id) => {
  return async () => {
    const res = await getArticleInfoApi(id)
    return res.data.data
  }
}

//编辑文章
export const editArticle = (draft, data) => {
  return async () => {
    await editArticleApi(draft, data)
  }
}
